Operating Systems No. 9 ศร ณย อ นทโกส ม Sarun Intakosum 1
Virtual-Memory Management 2
Background Virtual memory separation of user logical memory from physical memory. Only part of the program needs to be in memory for execution Logical address space can therefore be much larger than physical address space Allows address spaces to be shared by several processes Allows for more efficient process creation Virtual memory can be implemented via: Demand paging Demand segmentation 3
Virtual Memory That is Larger Than Physical Memory 4
Demand Paging Bring a page into memory only when it is needed Less I/O needed Less memory needed Faster response More users Page is needed reference to it invalid reference abort not-in-memory bring to memory Lazy swapper never swaps a page into memory unless page will be needed Swapper that deals with pages is a pager 5
Valid-Invalid Bit With each page table entry a valid invalid bit is associated (v in-memory,i not-in-memory) Initially valid invalid bit is set toi on all entries Example of a page table snapshot: Frame # valid-invalid bit v v v v i. page table i i During address translation, if valid invalid bit in page table entry isi page fault 6
Page Table When Some Pages Are Not in Main Memory 7
Page Fault If there is a reference to a page, first reference to that page will trap to operating system: page fault 1. Operating system looks at another table to decide: Invalid reference abort Just not in memory 2. Get empty frame 3. Swap page into frame 4. Reset tables 5. Set validation bit = v 6. Restart the instruction that caused the page fault 8
Steps in Handling a Page Fault 9
What happens if there is no free frame? Page replacement find some page in memory, but not really in use, swap it out algorithm performance want an algorithm which will result in minimum number of page faults Same page may be brought into memory several times 10
Page Replacement Algorithms Want lowest page-fault rate Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string 11
Graph of Page Faults Versus The Number of Frames 12
FIFO Page Replacement 13
FIFO Illustrating Belady s Anomaly 14
Optimal Algorithm Replace page that will not be used for longest period of time How do you know this? Used for measuring how well your algorithm performs 15
Optimal Page Replacement 16
LRU Page Replacement 17
Global vs. Local Allocation Global replacement process selects a replacement frame from the set of all frames; one process can take a frame from another Local replacement each process selects from only its own set of allocated frames 18
Thrashing Thrashing a process is busy swapping pages in and out If a process does not have enough pages, the page-fault rate is very high. This leads to: low CPU utilization operating system thinks that it needs to increase the degree of multiprogramming another process added to the system 19
Thrashing (Cont.) Operation becomes inefficient Caused when a page is removed from memory but iscalled back shortly thereafter Can occur across jobs, when a large number of jobs arevying for a relatively few number of free pages Can happen within a job (e.g., in loops that cross pageboundaries) 20
The Working Set Working set:setof pages residing in memory that can be accessed directly without incurring a page fault Improves performance of demand page schemes Requires the concept of locality of reference System must decide How many pages compose the working set The maximum number of pages the operating system willallow for a working set 21
Working-Set Model working-set window a fixed number of page references Example: 10,000 instruction WSS i (working set of Process P i ) = total number of pages referenced in the most recent (varies in time) if too small will not encompass entire locality if too large will encompass several localities if = will encompass entire program D= WSS i total demand frames if D> m Thrashing Policy if D> m, then suspend one of the processes 22
Working-set model 23
Keeping Track of the Working Set Approximate with interval timer + a reference bit Example: = 10,000 Timer interrupts after every 5000 time units Keep in memory 2 bits for each page Whenever a timer interrupts copy and sets the values of all reference bits to 0 If one of the bits in memory = 1 page in working set Why is this not completely accurate? Improvement = 10 bits and interrupt every 1000 time units 24
Program Structure Program structure int[128,128] data; Each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i,j] = 0; 128 x 128 = 16,384 page faults Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0; 128 page faults 25
Disk Management 26
Disk Scheduling The operating system is responsible for using hardware efficiently for the disk drives, this means having a fast access time and disk bandwidth. Access time has two major components Seek timeis the time for the disk are to move the heads to the cylinder containing the desired sector. Rotational latencyis the additional time waiting for the disk to rotate the desired sector to the disk head. Minimize seek time Seek time seek distance Disk bandwidth is the total number of bytes transferred, divided by the total time between the first request for service and the completion of the last transfer. 27
FCFS Illustration shows total head movement of 640 cylinders. 28
Disk Scheduling Several algorithms exist to schedule the servicing of disk I/O requests. We illustrate them with a request queue (0-199). 98, 183, 37, 122, 14, 124, 65, 67 Head pointer 53 29
SSTF Selects the request with the minimum seek time from the current head position. SSTF scheduling is a form of SJF scheduling; may cause starvation of some requests. Illustration shows total head movement of 236 cylinders. 30
SSTF (Cont.) 31
SCAN The disk arm starts at one end of the disk, and moves toward the other end, servicing requests until it gets to the other end of the disk, where the head movement is reversed and servicing continues. Sometimes called the elevator algorithm. Illustration shows total head movement of 208 cylinders. 32
SCAN (Cont.) 33
C-SCAN Provides a more uniform wait time than SCAN. The head moves from one end of the disk to the other. servicing requests as it goes. When it reaches the other end, however, it immediately returns to the beginning of the disk, without servicing any requests on the return trip. Treats the cylinders as a circular list that wraps around from the last cylinder to the first one. 34
C-SCAN (Cont.) 35
C-LOOK Version of C-SCAN Arm only goes as far as the last request in each direction, then reverses direction immediately, without first going all the way to the end of the disk. 36
C-LOOK (Cont.) 37
Selecting a Disk-Scheduling Algorithm SSTF is common and has a natural appeal SCAN and C-SCAN perform better for systems that place a heavy load on the disk. Performance depends on the number and types of requests. Requests for disk service can be influenced by the file-allocation method. The disk-scheduling algorithm should be written as a separate module of the operating system, allowing it to be replaced with a different algorithm if necessary. Either SSTF or LOOK is a reasonable choice for the default algorithm. 38
File Management 39
File Operations File is an abstract data type Create Write Read Reposition within file Delete Truncate Open(F i ) search the directory structure on disk for entry F i, and move the content of entry to memory Close (F i ) move the content of entry F i in memory to directory structure on disk 40
File Types Name, Extension 41
Directory Structure A collection of nodes containing information about all files Directory Files F 1 F 2 F 3 F 4 F n Both the directory structure and the files reside on disk Backups of these two structures are kept on tapes 42
Operations Performed on Directory Search for a file Create a file Delete a file List a directory Rename a file Traverse the file system 43
Tree-Structured Directories 44
Tree-Structured Directories (Cont) Efficient searching Grouping Capability Current directory (working directory) cd /spell/mail/prog type list 45
Tree-Structured Directories (Cont) Absolute or relative path name Creating a new file is done in current directory Delete a file rm <file-name> Creating a new subdirectory is done in current directory mkdir <dir-name> Example: if in current directory /mail mkdir count mail prog copy prt exp count Deleting mail deleting the entire subtree rooted by mail 46
File System Mounting A file system must be mountedbefore it can be accessed 47
(a) Existing. (b) Unmounted Partition 48
Mount Point 49
File Sharing Sharing of files on multi-user systems is desirable Sharing may be done through a protection scheme On distributed systems, files may be shared across a network Network File System (NFS) is a common distributed file-sharing method 50
File Sharing Multiple Users User IDsidentify users, allowing permissions and protections to be per-user Group IDsallow users to be in groups, permitting group access rights 51
Protection File owner/creator should be able to control: what can be done by whom Types of access Read Write Execute Append Delete List 52
Access Lists and Groups Mode of access: read, write, execute Three classes of users RWX a) owner access 7 111 RWX b) group access 6 110 RWX c) public access 1 001 Ask manager to create a group (unique name), say G, and add some users to the group. For a particular file (say game) or subdirectory, define an appropriate access. owner group public Attach a group to a file chmod 761 game chgrp G game 53
Windows XP Access-control List Management 54
A Sample UNIX Directory Listing 55
File Allocation Methods An allocation method refers to how disk blocks are allocated for files: Contiguous allocation Linked allocation Indexed allocation 56
Contiguous Allocation Each file occupies a set of contiguous blocks on the disk Simple only starting location (block #) and length (number of blocks) are required Random access Wasteful of space (dynamic storage-allocation problem) Files cannot grow 57
Contiguous Allocation of Disk Space 58
Linked Allocation Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk. block = pointer 59
Linked Allocation 60
File-Allocation Table 61
Indexed Allocation Brings all pointers together into the index block. Logical view. index table 62
Example of Indexed Allocation 63
Indexed Allocation (Cont.) Need index table Random access Dynamic access without external fragmentation, but have overhead of index block. Mapping from logical to physical in a file of maximum size of 256K words and block size of 512 words. We need only 1 block for index table. 64
Indexed Allocation Mapping (Cont.) Mapping from logical to physical in a file of unbounded length (block size of 512 words). Linked scheme Link blocks of index table (no limit on size). 65
Indexed Allocation Mapping (Cont.) Two-level index (maximum file size is 512 3 ) M outer-index index table file 66
Combined Scheme: UNIX (4K bytes per block) 67